//
//  PayVC.m
//  夺宝吧
//
//  Created by xiaocai on 15/6/29.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "PayVC.h"
#import "PayCell.h"
#import "PublicBackBtn.h"
#import "OpenAllCell.h"
#import "PayFooter.h"
#import "WYAlertView.h"
#import "ShoppingCarVC.h"

@interface PayVC ()<UITableViewDataSource,UITableViewDelegate , UIAlertViewDelegate>
{
    //表格
    UITableView *_tableView;
    //表尾
    PayFooter *_footer;
    //展开所有商品标识
    BOOL isOpenFlag;
}
@property (nonatomic , assign) NSInteger count;
@end

@implementation PayVC
- (instancetype)init
{
    if (self = [super init]) {
        self.data = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    [ShoppingCarVC shareShoppingCarVC].firstFlag = YES;
    self.count = 0;
    self.view.backgroundColor = [UIColor whiteColor];
    isOpenFlag = NO;
    [self loadCustomNaviBar];
    [self loadTableView];
    //加载底部支付按钮
    [self loadPayBtn];
    
    [self syncFlat];
    
     [_footer setDefaultPay];
    [_footer setLineView];
    
    //支付宝
    if (self.zfbflag == 0) {
        [self changeFrame];
        [_footer changeFrame];
    }
    //爱贝支付宝
    if (self.iapppayFlagAlipay == 0){
        [self changeFrame];
        [_footer changeFrame];
        
    }
        //pingpp京东
    if (self.jdflag == 0) {
        [self changeFrame];
        [_footer changeFrame];
        
    }
    //爱贝微信
    if (self.iapppayflag == 0) {
        [self changeFrame];
        [_footer changeFrame];
        
    }

    
//    [_footer ifNoAll];
   
}

- (void)changeFrame{
//    self.count = 0;
    
    if (self.count <1){
        //支付宝
        [_footer changeZFB];
        //爱贝支付宝
        [_footer changeIAPPPayAlipay];
        
        //pingpp京东
        [_footer changeJD];
        //爱贝微信
        [_footer changeIAPPPay];

    }
    self.count ++;
}

- (void)syncFlat{
    _footer.zfbflag = self.zfbflag;
    _footer.jdflag = self.jdflag;
    _footer.iapppayflag = self.iapppayflag;
    _footer.iapppayFlagAlipay = self.iapppayFlagAlipay;
}
//加载导航条
- (void)loadCustomNaviBar
{
    UIView *naviBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WYMWITH, 20+44)];
    naviBar.backgroundColor = [UIColor whiteColor];
    //返回按钮
    UIButton *backBtn = [UIButton buttonWithType:0];
    backBtn.frame = CGRectMake(10, 25, 30, 30);
    [backBtn setImage:[UIImage imageNamed:@"icon_back_press.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [naviBar addSubview:backBtn];
    //标题
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 25)];
    titleLab.center = CGPointMake(WYMWITH/2, 42);
    titleLab.text = @"结算支付";
    titleLab.font = [UIFont systemFontOfSize:TITLE_SIZE];
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.textColor = [UIColor blackColor];
    //分割线
    [naviBar addSubview:[WYPublic getFenGexian:0 :naviBar.frame.size.height-0.5 :WYMWITH :0.5]];
    [naviBar addSubview:titleLab];
    [self.view addSubview:naviBar];
}

//返回按钮
- (void)backBtnOnClick:(UIButton *)sender
{
    
    //弹出对话提示
//    WYAlertView *altView = [WYAlertView showInView:self.view withTitle:@"温馨提示：" message:@"您确定取消订单吗？" confirmButtonTitle:@"确定" cancelButtonTitle:@"取消"];
//    [altView handleCancel:^{
//       
//    } handleConfirm:^{
//        [self cancelOrder];
//        
//    }
//    ];
//    [altView show];
    
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"您确定取消订单吗?" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    self.alertView = alertView;
    [alertView show];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (alertView == self.alertView){
        if (buttonIndex == 0){
            [self cancelOrder];
        }
    }
    
}

//取消订单
-(void)cancelOrder
{
    //取消订单
    NSDictionary *params = @{@"ids":self.orderId,@"uid":[UserInfo getUserID],@"token":[UserInfo getUserToken],@"refresh_token":[UserInfo getRefreshToken]};
    [WYPublic addHudToWindowWithTitle:@"正在取消订单..."];
    [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,DEL_ORDER_URL] Parameters:params successBlock:^(NSDictionary *dic) {
//        [ShoppingCarVC shareShoppingCarVC].firstFlag = YES;
        [WYPublic removeHudToWindow];
        
        [self.navigationController popViewControllerAnimated:YES];
        
    } failedBlock:^{
//        [ShoppingCarVC shareShoppingCarVC].firstFlag = YES;
        [WYPublic removeHudToWindow];
        [self.navigationController popViewControllerAnimated:YES];
    } needLog:NO];

}
//改写setdata方法
-(void)setData:(NSMutableArray *)data
{
    _data = data;
    
    [_tableView reloadData];
}
//加载表格
- (void)loadTableView
{
    // update by weienjie 20150918
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, WYMWITH, WYMHEIGHT - 64 -60)];
    // end by weienjie
    _tableView.dataSource = self;
    _tableView.delegate = self;
    //注册cell
    [_tableView registerNib:[UINib nibWithNibName:@"PayCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [_tableView registerNib:[UINib nibWithNibName:@"OpenAllCell" bundle:nil] forCellReuseIdentifier:@"openCell"];
    _tableView.rowHeight = 45.0;
    _tableView.tableFooterView = [[UIView alloc]init];
    _tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //表尾部
    _footer = [[[NSBundle mainBundle]loadNibNamed:@"PayFooter" owner:self options:nil]lastObject];
    //交易信息
    _footer.allPrice = self.allPrice;
    _footer.paycode = self.paycode;
    _footer.orderId = self.orderId;
    //将支付模型传给footer
    _footer.zfbModel = self.zfbModel;
    _footer.iapppayWXModel = self.iapppayWXModel;
    _footer.iapppayzfbModel = self.iapppayzfbModel;
    _footer.pingppJDModel  = self.pingppJDModel;
    
    _tableView.tableFooterView = _footer;
    [self.view addSubview:_tableView];
}
#pragma mark -- 表格协议函数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (isOpenFlag ) {
        if (_data.count>3) {
            return _data.count+1;
        }else{
            return _data.count;
        }
    }else{
        if (_data.count <= 3) {
            return _data.count;
        }
    }
    return 3+1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == _data.count && _data.count > 3) {
        OpenAllCell *cell = [tableView dequeueReusableCellWithIdentifier:@"openCell"];
        [cell changePicWithOpenFlag:isOpenFlag];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.row == 3 && isOpenFlag==NO) {
        OpenAllCell *cell = [tableView dequeueReusableCellWithIdentifier:@"openCell"];
        [cell changePicWithOpenFlag:isOpenFlag];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    PayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (_data.count > 0 ) {
        WYShopCarModel *model = _data[indexPath.row];
        [cell fillCellWithModel:model];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (isOpenFlag) {/*************展开按钮cell*****************/
        if (indexPath.row == _data.count) {
            isOpenFlag = NO;
        }
        [_tableView reloadData];
    }else{
        if (indexPath.row == 3) {
            isOpenFlag = YES;
        }
        [_tableView reloadData];
    }
}
//底部结算按钮
- (void)loadPayBtn
{
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, WYMHEIGHT - 60, WYMWITH, 60)];
    bgView.backgroundColor = [UIColor whiteColor];
    UIButton *payBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    payBtn.frame = CGRectMake(20, 10, WYMWITH - 40, 40);
    [payBtn setTitle:@"去支付" forState:UIControlStateNormal];
    [payBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [payBtn addTarget:self action:@selector(payBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    payBtn.backgroundColor = QBB_RED_COLOR;
    payBtn.layer.cornerRadius = 2;
    payBtn.layer.masksToBounds = YES;
    [bgView addSubview:payBtn];
    [self.view addSubview:bgView];
}

//禁用返回
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    // 禁用 iOS7 返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    // 开启
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}
//支付按钮的监听事件
- (void)payBtnOnClick
{
//    [ShoppingCarVC shareShoppingCarVC].firstFlag = YES;
    [_footer payBtnOnClick];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc{
    
    NSLog(@"PayVC dealloc");
}

@end
